package com.dongdong.controller;

import com.dongdong.em.Result;
import com.dongdong.model.TbItemCat;
import com.dongdong.page.PageParams;
import com.dongdong.service.ItemCatService;
import com.dongdong.service.TypeTemplateService;
import com.dongdong.vo.ItemCatVO;
import com.dongdong.vo.ItemVO;
import com.dongdong.vo.TypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("itemcat")
@RefreshScope   //热更新
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    @Autowired
    private TypeTemplateService typeTemplateService;

    @GetMapping("getItemCatByParentId")
    public Result getItemCatByParentId(@RequestParam Long parentId){
        List<ItemCatVO> itemCatVOList = itemCatService.getItemCatByParentId(parentId);
        return Result.OK().setData(itemCatVOList);
    }

    @PostMapping("getItemParentAndPage")
    public Result getItemParentXiaji(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,Long parentid){
        PageParams<TbItemCat> itemCatvoPage = itemCatService.getItemParentXiaji(pageNum,pageSize,parentid);
        return Result.OK().setData(itemCatvoPage);
    }

    @GetMapping("getInsertAndTypeId")
    public Result getInsertAndTypeId(){
        //将type全部查询出来展示在增加页面  然后选择模板后将type_id添加到itemcat进行关联
        //调接口
        List<TypeVO> typeVOS = typeTemplateService.getTypeListAll();
        return Result.OK().setData(typeVOS);
    }

    @PostMapping("InsertItemCat")
    public Result InsertItemCat(@RequestBody ItemCatVO itemCatVO){
        int i = itemCatService.InsertItemCat(itemCatVO);
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }

    @GetMapping("getItemCatById")
    public Result<ItemCatVO> getItemCatById(@RequestParam Long id){
        ItemCatVO vo = itemCatService.getItemCatById(id);
        return Result.OK().setData(vo);
    }


    //商品管理表  根据表中的parentId 查询数据  然后获取第一条
    @GetMapping("getPatentIdShow")
    public Result getPatentIdShow(@RequestParam Long parenId){
        ItemCatVO itemCatVO = itemCatService.getPatentIdShow(parenId);
        return Result.OK().setData(itemCatVO);
    }


    //根据类目itemcat表的父id获取到模板id    然后根据模板id   查询全部获取模板品牌  填充到下拉菜单中
    @GetMapping("getBrand")
    public Result getBrand(@RequestParam Long id){//parentId
        TypeVO typeTemplateVO = itemCatService.getBrand(id);
        return Result.OK().setData(typeTemplateVO);
    }

}
